{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "导入 NumPy："
      ],
      "metadata": {
        "id": "A4TOCxSoQoW8"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "id": "T5o7obeTQTJT"
      },
      "outputs": [],
      "source": [
        "import numpy as np"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "NumPy 核心数据结构"
      ],
      "metadata": {
        "id": "QTKyfIvoQt27"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "arr = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64)\n",
        "print(arr.dtype)  # 数据类型: float64\n",
        "print(arr.shape)  # 数组形状: (2, 3)\n",
        "print(arr.size)   # 元素总数: 6\n",
        "print(arr.ndim)   # 数组维度: 2\n",
        "print(arr.itemsize)  # 单个元素大小（字节）: 8\n",
        "print(arr.data)   # 内存存储地址"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "mczgCTz4Qg_D",
        "outputId": "34c17997-d311-4fe9-90c7-4315685b4710"
      },
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "float64\n",
            "(2, 3)\n",
            "6\n",
            "2\n",
            "8\n",
            "<memory at 0x7fd3e5fa3e00>\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "从列表或元组创建数组（用于转换现有数据结构）\n"
      ],
      "metadata": {
        "id": "RwF1561FQw8c"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "arr1 = np.array([1, 2, 3, 4])\n",
        "arr2 = np.array([[1, 2, 3], [4, 5, 6]])  # 二维数组"
      ],
      "metadata": {
        "id": "WS46Wg-uQjOz"
      },
      "execution_count": 3,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "创建特定数组（用于初始化特定形状的数据）"
      ],
      "metadata": {
        "id": "jNsMHbNoQzTC"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "zeros = np.zeros((2, 3))  # 创建 2x3 全零数组\n",
        "ones = np.ones((3, 3))    # 创建 3x3 全 1 数组\n",
        "identity = np.eye(4)      # 创建 4x4 单位矩阵\n",
        "arange_arr = np.arange(0, 10, 2)  # 生成 0 到 10，步长 2 的数组\n",
        "linspace_arr = np.linspace(0, 1, 5)  # 生成 0 到 1 的 5 个均匀数值\n",
        "random_arr = np.random.rand(3, 3)  # 生成 3x3 随机数组"
      ],
      "metadata": {
        "id": "vphTNyL7Q2ji"
      },
      "execution_count": 4,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "数组形状操作（用于改变数据结构）"
      ],
      "metadata": {
        "id": "hm9x0NSVQ5Db"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "arr = np.array([[1, 2, 3], [4, 5, 6]])\n",
        "print(arr.shape)  # (2, 3)\n",
        "print(arr.reshape(3, 2))  # 重新调整形状\n",
        "print(arr.flatten())  # 转换为一维数组"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "_Q6tP9pMQ787",
        "outputId": "26fcfae1-09da-4a06-99c6-af9863ef95fc"
      },
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "(2, 3)\n",
            "[[1 2]\n",
            " [3 4]\n",
            " [5 6]]\n",
            "[1 2 3 4 5 6]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "访问数组元素（用于数据提取）"
      ],
      "metadata": {
        "id": "OtpFBMICRA6T"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "print(arr[0, 1])  # 获取第一行第二列元素\n",
        "print(arr[:, 1])  # 获取第二列所有元素\n",
        "print(arr[0, :])  # 获取第一行所有元素\n",
        "print(arr[1, -1])  # 获取第二行最后一个元素"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "mp_fqLYRRAVL",
        "outputId": "a16cf6d3-9f40-4fc8-e872-47a64b2d3d1f"
      },
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "2\n",
            "[2 5]\n",
            "[1 2 3]\n",
            "6\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "数组运算（用于数学计算和矩阵操作）"
      ],
      "metadata": {
        "id": "qy3EX5HARGP6"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "arr1 = np.array([1, 2, 3])\n",
        "arr2 = np.array([4, 5, 6])\n",
        "print(arr1 + arr2)  # 数组加法\n",
        "print(arr1 - arr2)  # 数组减法\n",
        "print(arr1 * arr2)  # 逐元素乘法\n",
        "print(arr1 / arr2)  # 逐元素除法\n",
        "print(arr1 ** 2)    # 幂运算\n",
        "print(arr1 @ arr2)  # 矩阵乘法（点积）"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "WnTfjNxJRHUN",
        "outputId": "9d5094dc-536b-4223-aee8-48ef9963d0ff"
      },
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[5 7 9]\n",
            "[-3 -3 -3]\n",
            "[ 4 10 18]\n",
            "[0.25 0.4  0.5 ]\n",
            "[1 4 9]\n",
            "32\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "数组广播（用于不同形状数组的运算）"
      ],
      "metadata": {
        "id": "_O3YVsTZRJoj"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "arr1 = np.array([[1], [2], [3]])\n",
        "arr2 = np.array([4, 5, 6])\n",
        "print(arr1 + arr2)  # 通过广播机制自动扩展数组"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "nx9GyYYURN46",
        "outputId": "0b768b8e-9fcc-44b0-f1ee-eb6a2f8d876e"
      },
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[[5 6 7]\n",
            " [6 7 8]\n",
            " [7 8 9]]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "索引和切片的高级应用（布尔索引、花式索引）"
      ],
      "metadata": {
        "id": "Sc8Z01-nRQyM"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "arr = np.array([10, 20, 30, 40, 50])\n",
        "print(arr[arr > 25])  # 选取大于 25 的元素\n",
        "indices = [0, 2, 4]\n",
        "print(arr[indices])  # 选取特定索引的元素"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "qCNTa3tBRRYj",
        "outputId": "b9f00bec-1c4b-4588-a546-379bb484b81a"
      },
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[30 40 50]\n",
            "[10 30 50]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "拼接与拆分（用于数据重组）"
      ],
      "metadata": {
        "id": "oU4mNmIRRWVq"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "arr1 = np.array([[1, 2], [3, 4]])\n",
        "arr2 = np.array([[5, 6]])\n",
        "print(np.vstack((arr1, arr2)))  # 垂直拼接\n",
        "print(np.hstack((arr1, arr2.T)))  # 水平拼接"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "LVYc-6wQRW3r",
        "outputId": "71f346c7-cf6a-4cc8-c2d7-e3e17bfc3e88"
      },
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[[1 2]\n",
            " [3 4]\n",
            " [5 6]]\n",
            "[[1 2 5]\n",
            " [3 4 6]]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "统计与数学函数"
      ],
      "metadata": {
        "id": "RDz25VgKRbeS"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "arr = np.array([1, 2, 3, 4, 5])\n",
        "print(np.mean(arr))  # 均值\n",
        "print(np.std(arr))  # 标准差\n",
        "print(np.max(arr))  # 最大值"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "BUhBUDKERbqi",
        "outputId": "8b8d0100-9f25-4fb9-b85a-beb3522d450c"
      },
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "3.0\n",
            "1.4142135623730951\n",
            "5\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "排序和搜索"
      ],
      "metadata": {
        "id": "p5sPj8NHRgRi"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "arr = np.array([3, 1, 5, 2, 4])\n",
        "print(np.sort(arr))  # 排序\n",
        "print(np.argsort(arr))  # 返回排序后的索引"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "8Xe_eYXzRiUi",
        "outputId": "ec13e11a-3d15-44ed-c61d-383a24bdf01b"
      },
      "execution_count": 12,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[1 2 3 4 5]\n",
            "[1 3 0 4 2]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "随机数生成"
      ],
      "metadata": {
        "id": "4zyWBeflRkx0"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "print(np.random.randint(0, 10, (2, 3)))  # 生成 2x3 的随机整数数组"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "VcxSIfTwRld0",
        "outputId": "6450ef65-c307-4c11-ebf2-819525aacc5a"
      },
      "execution_count": 13,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[[3 1 8]\n",
            " [2 2 5]]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "NumPy 与文件操作（用于数据存储与加载）"
      ],
      "metadata": {
        "id": "ll4Ww-5tRpiz"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "np.save('array.npy', arr)  # 保存 NumPy 数组\n",
        "loaded_arr = np.load('array.npy')  # 读取 NumPy 数组\n",
        "print(loaded_arr)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "nP983iRTRslV",
        "outputId": "50e2c1db-3292-4df0-cade-dac82c6f6f52"
      },
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[3 1 5 2 4]\n"
          ]
        }
      ]
    }
  ]
}